package ${project.packageName}.${project.dataCenterType}.ctrl;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import ${project.packageName}.${project.dataCenterType}.client.HbaseConvertUtil;
import ${project.packageName}.${project.dataCenterType}.client.HbaseOperateClient;
import ${project.packageName}.${project.dataCenterType}.constant.BaseConstant;
import ${project.packageName}.${project.dataCenterType}.entity.JsonResult;
import ${project.packageName}.${project.dataCenterType}.util.ValidateRequestUtil;

@RestController
@RequestMapping("/${project.version}")
public class ${ctrlName}Controller {
    
    @Autowired
    private  HbaseOperateClient hbaseOperateClient;
    
    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(${ctrlName}Controller.class);
    
     /**
     * Description: ${restApi.description}
     <#list paraMaps as para>
     * @param ${para.name}
     </#list>
     */
    @RequestMapping(value = "${restApi.requestUrl}", method = RequestMethod.${restApi.requestType})
    public Object ${methodName}( HttpServletRequest request<#list paraMaps as para>, @RequestParam(required=${para.isMust}) ${para.type} ${para.name}</#list> ){
         if (!ValidateRequestUtil.validate(request)) {
            return new JsonResult(BaseConstant.CODE_TOKEN_ILLEGAL, BaseConstant.MSG_TOKEN_ILLEGAL);
        }
        
        try {
        
        
        Map<String, Integer> columnMap = new HashMap<String, Integer>();
        <#list returnValMaps as returnVal>
        columnMap.put("${returnVal.name}", ${returnVal.cellName});
        </#list>
        
         ${rowkeyStatements}
         
          Result result = hbaseOperateClient.getResult(rowkey, "${restApi.tableName}"+ operationTime.substring(0, 6));
        if (result == null || result.isEmpty()) {
                return new JsonResult(BaseConstant.CODE_DATA_NONEXIST,
                    BaseConstant.MSG_DATA_NONEXIST);
            }
        JSONObject returnObj = HbaseConvertUtil.result2JSONObj(columnMap, result, "${familyName}", "${qualifierName}");
        if (returnObj == null) {
                return new JsonResult(BaseConstant.CODE_DATA_NONEXIST,
                    BaseConstant.MSG_DATA_NONEXIST);
            }
            return JSON.toJSONString(returnObj);
        }
        catch (Exception e) {
            LOG.error(e.getMessage());
            return new JsonResult(BaseConstant.CODE_ERROR, BaseConstant.MSG_ERROR);
        }
    }
}
